<template>
	<view class="container home-pd">
		<view class="card">
			<uv-form labelPosition="left" ref="form">
				<uv-form-item label-width="200rpx" label="关联批次">
					 {{RturnDetailsData.batchId}}
				</uv-form-item>
				<uv-form-item label-width="200rpx" label="批次当前库存">
					{{RturnDetailsData.stockQuantity}}
				</uv-form-item>
			</uv-form>
		</view>
		<view class="card">
			<uv-form labelPosition="left" ref="form">
				<uv-form-item label-width="200rpx" label="退货数量">
					<uv-input v-model="params.quantityReturned" placeholder="请输入退货数量" border="none" />
				</uv-form-item>
				<uv-form-item label-width="200rpx" label="采购说明">
					<uv-input v-model="params.returnDescribe" placeholder="请输入采购说明" border="none" />
				</uv-form-item>
			</uv-form>
		</view>
		<view class="tip">
			注意：退货数量不得超过批次当前库存，且退货后无法恢复，请谨慎操作。
		</view>
		<view class="button-group">
			<view @click="submit" :class="{loading:loading}"  class=" bt-1 flex flex-justify-center flex-items-center">
				<uv-loading-icon color="#FFFFFF" v-if="loading" />
				<view v-else>确认退货</view>
			</view>
			<view @click="uni.navigateBack()" class="bt-2 flex flex-justify-center flex-items-center">暂不退货</view>
		</view>
	</view>
</template>

<script setup>
	import {getRturnDetails,returnGoods} from '@/api/task.js';
	let loading = ref(false);
	let item;
	let RturnDetailsData = ref({});
	let params = reactive({
		quantityReturned:"",
		returnDescribe:"",
		id:""
	})
	onLoad((option) => {
		if (option.item) {
			item = JSON.parse(decodeURIComponent(option.item));
			params.id = item.id;
			init();
		}
	})
	function init(){
		getRturnDetails({
			providerTaskId:item.id
		}).then(res=>{ 
			 ({list:[RturnDetailsData.value={}]}= res.data ||{});  
		})
	}
	function submit(){ 
		if(loading.value) return;
		loading.value=true;  
		returnGoods(params).then(res=>{
			if(res.code==0){
				uni.showToast({
					title:"操作成功",
					icon:"none"
				});
				setTimeout(()=>{ 
					uni.switchTab({
						url:'/pages/task/index'
					})
				},1000)
			}
		}).finally(()=>{
			setTimeout(()=>{
				loading.value=false;
			},1000)
		})
	}
</script>

<style lang="scss" scoped>
	@import 'index.scss';
</style>